#pragma once
#include <string>
#include "inetaddress.h"
#include <arpa/inet.h>
#include <boost/lexical_cast.hpp>
namespace TinyRPC{
INetAddress::INetAddress(){
    
}

std::string INetAddress::toString() const{
    return m_ip + ":" + boost::lexical_cast<std::string>(m_port);
}
INetAddress::INetAddress( std::string ip, int port)
: m_ip(ip)
, m_port(port){
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(m_port);
    int addr = 0;
    inet_pton(AF_INET, m_ip.c_str(), &addr);
    m_addr.sin_addr.s_addr = addr;
}
sockaddr_in INetAddress::getSockAddr() const{
    return m_addr;
}

INetAddress::INetAddress(sockaddr_in addr)
:m_addr(addr)
{
    char buf[32];
    m_port = ntohs(addr.sin_port);
    inet_ntop(AF_INET, &addr.sin_addr.s_addr, buf, sizeof(buf));
    m_ip = std::string(buf);
}

}